package jjn.carl.greedy;

import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/8/1 13:25
 */
public class LeetCode860 {
    public boolean lemonadeChange(int[] bills) {
        int fiveCount = 0, tenCount = 0;
        for (int bill : bills) {
            if (bill == 5) {
                fiveCount++;
                continue;
            }
            if (bill == 10) {
                fiveCount--;
                tenCount++;
                if (fiveCount < 0) {
                    return false;
                }
                continue;
            }
            if (bill == 20) {
                if (tenCount > 0) {
                    tenCount--;
                    fiveCount--;
                    if (fiveCount < 0) {
                        return false;
                    }
                    continue;
                }
                fiveCount -= 3;
                if (fiveCount < 0) {
                    return false;
                }
            }
        }
        return true;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int total = scanner.nextInt();
        int[] bills = new int[total];
        for (int i = 0; i < total; i++) {
            bills[i] = scanner.nextInt();
        }
        boolean lemonadeChange = new LeetCode860().lemonadeChange(bills);
        System.out.println(lemonadeChange);
    }
}
